package bigapps.myneighbor.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.google.appengine.api.datastore.Blob;

import bigapps.myneighbor.dao.StageDao;
import bigapps.myneighbor.domain.Stage;
import bigapps.myneighbor.domain.StageEnum;

@Repository(value = "stageDao")
public class StageDaoImpl implements StageDao {
	private EntityManager em;
	public EntityManager getEntityManager(){
		return em;
	}
	
	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	public void setEntityManager(EntityManager em)
	{
		this.em = em;
	}
	
	@Transactional
	public void saveParkingFacility(String fileName, Blob content){
		Stage stage = new Stage();
		stage.setType(StageEnum.PARKINGFACILITY.name());
		stage.setFileName(fileName);
		stage.setContent(content);
		em.persist(stage);
	}
	
	public Blob getParkingFacility(){
		System.out.println("StageDao.getParkingFacility called");
		Query query = em.createQuery("select s from stage s where s.type = :type");
		query.setParameter("type", StageEnum.PARKINGFACILITY.name());
		Stage stage = (Stage)query.getSingleResult();
		System.out.println(stage.getFileName());
		System.out.println(stage.getType());
		return stage.getContent();
	}
	public List<Stage> findAll(){
		Query query = em.createQuery("select s from stage s");
		return query.getResultList();
	}
}
